package fivestar.model.dao.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import fivestar.model.dao.GuestDao;
import fivestar.model.entity.Guest;
import fivestar.util.HibernateUtil;

public class GuestDaoHibernate implements GuestDao {

	Session session = null;
	Transaction tx = null;

	@Override
	public void create(Guest guest) throws RuntimeException {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			session.save(guest);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}

	}

	@Override
	public void delete(Guest guest) throws RuntimeException {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			session.delete(guest);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}

	}

	@Override
	public void update(Guest guest) throws RuntimeException {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			session.update(guest);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}

	}

	@Override
	public Guest find(String dni) throws RuntimeException {
		Guest guest = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Guest where dni = :dni");
			query.setString("dni", dni);
			guest = (Guest) query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return guest;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> findAll() throws RuntimeException {
		 int tamanyoPagina = 10;
		 int paginaAMostrar = 7;
		 
		List<Object[]> lista = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			//lista = session.getNamedQuery("findAllGuestAddress").list();
			Query query = session.getNamedQuery("findAllGuestAddress");
			query.setMaxResults(tamanyoPagina);
			query.setFirstResult(paginaAMostrar * tamanyoPagina);
			lista = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> findlByDni(String dni) throws RuntimeException {
		String temp = dni + "%";
		List<Object[]> lista = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("select g.dni, g.lastName, g.firstName from Guest g where g.dni like ? order by g.dni");
			 query.setString(0, temp);			 
			 lista =  query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> findlByLastName(String lastName) throws RuntimeException {
		String temp = lastName + "%";
		List<Object[]> lista = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("select g.dni, g.lastName, g.firstName from Guest g where g.lastName like ? order by g.lastName");
			 query.setString(0, temp);			 
			 lista =  query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return lista;
	}

}
